//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package third.lib.wx.outpay.redpacket;

import com.alibaba.fastjson.JSON;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.utils.StringUtils;
import third.lib.utils.XMLUtils;
import third.lib.wx.common.utils.WXUtils;
import third.lib.wx.outpay.redpacket.model.WxOutPayRedPacketRet;
import third.lib.wx.outpay.redpacket.config.WxOutPayRedPacketConfig;

public class WxOutPayRedPacket {
    private final Logger logger = LoggerFactory.getLogger(WxOutPayRedPacket.class);
    @Autowired
    ThirdLogger thirdLogger;
    @Autowired
    WxOutPayRedPacketConfig config;

    public WxOutPayRedPacket() {
    }

    public WxOutPayRedPacketRet sendRedPacket(String nonceStr, String tradeNo, String openid, String amount, String sendName, String wishing, String actName, String remark) throws ThirdException {
        thirdLogger.info("sendRedPacket: " + JSON.toJSONString(this.config));
        SortedMap<String, Object> sortedMap = new TreeMap();
        sortedMap.put("wxappid", this.config.getAppId());
        sortedMap.put("mch_id", this.config.getMchId());
        sortedMap.put("nonce_str", nonceStr);
        sortedMap.put("mch_billno", tradeNo);
        sortedMap.put("send_name", sendName);
        sortedMap.put("re_openid", openid);
        sortedMap.put("total_amount", amount);
        sortedMap.put("total_num", 1);
        sortedMap.put("wishing", wishing);
        sortedMap.put("client_ip", this.config.getIp());
        sortedMap.put("act_name", actName);
        sortedMap.put("remark", remark);

        String sign;
        try {
            sign = WXUtils.sign(sortedMap, this.config.getKey());
        } catch (ThirdException var17) {
            logger.error(var17.getLocalizedMessage());
            throw var17;
        }

        String xmlStr = WXUtils.mapToStr(sortedMap, sign);

        String retStr;
        try {
            RestTemplate restTemplate = WXUtils.getRestTemplateWithCert(this.config.getCrtPath(), this.config.getMchId());
            retStr = restTemplate.postForObject("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", xmlStr, String.class);
        } catch (RestClientException var15) {
            logger.error(var15.getLocalizedMessage());
            throw new ThirdException(ThirdStatus.WX);
        } catch (ThirdException var16) {
            throw var16;
        }

        Map<String, Object> retMap = XMLUtils.prase(retStr);
        if (retMap == null) {
            throw new ThirdException(ThirdStatus.WX);
        } else {
            retMap = (Map)retMap.get("xml");
            if (!retMap.get("return_code").equals("SUCCESS")) {
                this.logger.error(retStr);
                throw new ThirdException(ThirdStatus.WX);
            } else if (!retMap.get("result_code").equals("SUCCESS")) {
                this.logger.error(retStr);
                throw new ThirdException(ThirdStatus.WX);
            } else {
                WxOutPayRedPacketRet outPayRet = new WxOutPayRedPacketRet();
                outPayRet.setSendListid(StringUtils.praseToStr(retMap.get("send_listid")));
                outPayRet.setTradeOut(StringUtils.praseToStr(retMap.get("mch_billno")));
                outPayRet.setTotalAmount(Integer.valueOf(StringUtils.praseToStr(retMap.get("total_amount"))));
                return outPayRet;
            }
        }
    }
}
